<?php 

    function getDirectory($path = '.', $prev = '', $role = 'php', $recursive = true, $echoPrev = '', $ignoreOut = array()){
        global $content;
        
        $ignore = array('cgi-bin', '.', '..', '.svn');
        $dh = opendir($path);
        while($file = readdir($dh)) {
            if(!in_array($file, $ignore) && !in_array($file, $ignoreOut)){
                if(is_dir("$path/$file")){
                    if ($recursive) {
                        getDirectory("$path/$file", ($prev == '' ? $file : "$prev/$file"), $role, $recursive, $echoPrev);
                    } 
                } else {
                    $content .= "$echoPrev<file role=\"$role\" baseinstalldir=\"/\" name=\"$prev/$file\"/>\n";
                }
            }
        }
        closedir($dh);
    }
    
    function getDirectorySmart($path = '.', $prev = '', $role = 'php', $recursive = true, $echoPrev = '', $ignoreOut = array()) {
        if (!empty($prev)) {
            $path = "$path/$prev";
        }
        
        getDirectory($path, $prev, $role, $recursive, $echoPrev, $ignoreOut);
    }

    $intelligencerProjectDir = dirname(dirname(__FILE__));
    
$content = 
'<?xml version="1.0"?>
<package version="2.0" xmlns="http://pear.php.net/dtd/package-2.0"
    xmlns:tasks="http://pear.php.net/dtd/tasks-1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0
http://pear.php.net/dtd/tasks-1.0.xsd
http://pear.php.net/dtd/package-2.0
http://pear.php.net/dtd/package-2.0.xsd">
    <name>Intelligencer</name>
    <channel>pear.drapeko.com</channel>
    <summary>Intelligencer simplifies autoloading</summary>
    <description>Intelligencer simplifies autoloading</description>
    <lead>
        <name>Roman Drapeko</name>
        <user>drapeko</user>
        <email>roman.drapeko@gmail.com</email>
        <active>yes</active>
    </lead>
    <date>2010-04-24</date>
    <version>
        <release>2.1.1</release>
        <api>2.1.1</api>
    </version>
    <stability>
        <release>stable</release>
        <api>stable</api>
    </stability>
    <license>BSD license</license>
    <notes>
        - fixed itTemplate critical andCreate method
        - examples 
    </notes>
    
    <contents>
      <dir name="/">
        <!-- source -->
';
        getDirectorySmart($intelligencerProjectDir, 'Intelligencer', 'php', true, '        ', array('IntelligencerConstants.php'));
        
$content .= '
        <!-- tests -->
';
        getDirectorySmart($intelligencerProjectDir, 'UnitTests', 'test', true,  '        ');
$content .= '
        <!-- doc -->
        <file role="doc" baseinstalldir="/" name="LICENSE"/>
        <file role="doc" baseinstalldir="/" name="README"/>
        
      </dir>
    </contents>
    <dependencies>
        <required>
            <php>
                <min>5.0</min>
            </php>
            <pearinstaller>
                <min>1.4.0</min>
            </pearinstaller>
        </required>
    </dependencies>
     <phprelease>
     </phprelease>
     <changelog>
     
        <release>
            <date>2010-04-18</date>
            <version>
                <release>2.1.0</release>
                <api>2.1.0</api>
            </version>
            <stability>
                <release>stable</release>
                <api>stable</api>
            </stability>
            <license>BSD license</license>
            <notes>
                Fully unit tested and ready for production usage
            </notes>
        </release>
     
        <release>
           <version>
               <release>2.0.0</release>
               <api>2.0.0</api>
           </version>
           <stability>
               <release>beta</release>
               <api>beta</api>            
           </stability>
           <date>2010-04-11</date>
           <license>BSD Lincense</license>
           <notes>
            Very important release. 3rd full refactoring. Framework created as a lib.
            All below versions are deprecated and should not be used.
           </notes>
       </release> 
    
        <release>
           <version>
               <release>1.2.1</release>
               <api>1.2.1</api>
           </version>
           <stability>
               <release>beta</release>
               <api>beta</api>            
           </stability>
           <date>2010-02-14</date>
           <license>BSD Lincense</license>
           <notes>
           </notes>
       </release> 

        <release>
           <version>
               <release>1.2.0</release>
               <api>1.2.0</api>
           </version>
           <stability>
               <release>beta</release>
               <api>beta</api>            
           </stability>
           <date>2009-01-20</date>
           <license>BSD Lincense</license>
           <notes>
           </notes>
       </release>     
     
        <release>
           <version>
               <release>1.1.4</release>
               <api>1.1.4</api>
           </version>
           <stability>
               <release>beta</release>
               <api>beta</api>            
           </stability>
           <date>2009-11-26</date>
           <license>BSD Lincense</license>
           <notes>
                This release solved the big bug in 1.1.3 related to incorrect parsing. 
                The decision was made to return the state of Intelligencer to beta for some period of time.
           </notes>
       </release>
         <release>
           <version>
               <release>1.1.3</release>
               <api>1.1.3</api>
           </version>
           <stability>
               <release>beta</release>
               <api>beta</api>            
           </stability>
           <date>2009-11-22</date>
           <license>BSD Lincense</license>
           <notes>
                There is a big bug in this version related to incorrect parsing of php string. 
                Please use a newer release instead.
           </notes>
       </release>
    
         <release>
           <version>
               <release>1.1.2</release>
               <api>1.1.2</api>
           </version>
           <stability>
               <release>beta</release>
               <api>beta</api>            
           </stability>
           <date>2009-10-30</date>
           <license>BSD Lincense</license>
           <notes>"Stable" release</notes>
       </release>
     
        <release>
           <version>
               <release>1.1.1</release>
               <api>1.1.1</api>
           </version>
           <stability>
               <release>beta</release>
               <api>beta</api>            
           </stability>
           <date>2009-10-24</date>
           <license>BSD Lincense</license>
           <notes>"Stable" release</notes>
       </release>
     
       <release>
           <version>
               <release>0.9.0</release>
               <api>0.9.0</api>
           </version>
           <stability>
               <release>beta</release>
               <api>beta</api>            
           </stability>
           <date>2009-09-09</date>
           <license>BSD Lincense</license>
           <notes>Beta release</notes>
       </release>
       
       <release>
           <version>
               <release>0.8.0</release>
               <api>0.8.0</api>
           </version>
           <stability>
               <release>alpha</release>
               <api>alpha</api>            
           </stability>
           <date>2009-08-07</date>
           <license>BSD Lincense</license>
           <notes>The first release for community</notes>
       </release>
     </changelog>
</package>
';

file_put_contents($intelligencerProjectDir.'/package.xml', $content);